package eShopDemo;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

@ManagedBean
public class Product {

	private String pid;
	private String name;
	private double price;

	// DO NOT new Catalog();
	@ManagedProperty(value = "#{catalog}")
	private Catalog catalog;

	@ManagedProperty(value = "#{cart}")
	private Cart cart;

	public Product() {
		;
	}

	public Product(String pid, String name, double price) {
		this.pid = pid;
		this.name = name;
		this.price = price;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		System.out.println("SET ID:" + pid);
		Product product = catalog.findProductById(pid);
		this.name = product.getName();
		this.pid = product.getPid();
		this.price = product.getPrice();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public Catalog getCatalog() {
		return catalog;
	}

	public void setCatalog(Catalog catalog) {
		this.catalog = catalog;
	}

	public Cart getCart() {
		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public String addToCart() {
		cart.add(pid);
		return "shoppingcart";
	}

}
